package autoTest.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jetty.util.log.Log;

import autoTest.device.Platform;
import autoTest.request.SetBlackListPart;
import main.ServerMain;
import net.sf.json.JSONArray;

public class FileInfoReader {
	
	public static int getFolderSize(File file) {
		String path = file.getAbsolutePath();
		int size = 0;
		for (String f : file.list()) {
			File childFile = new File(path + "/" + f);
			if (childFile.isDirectory()) {
				size = size + getFolderSize(childFile);
			} else {
				size ++;
			}
		}
		return size;
	}
	
	public static void readServerFiles(File file, JSONArray ja, Platform platform) throws IOException {
		System.out.println("readServerFiles path -> " + file.getAbsolutePath());
		if (!file.isDirectory()) {
			readFile(file, ja, platform);
		} else {
			readFolder(file, ja, platform);
		}
	}
	
	private static void readFile(File file, JSONArray ja, Platform platform) throws IOException {		//读取文件数据内容
		System.out.println("readFile path -> " + file.getAbsolutePath());
		Set<String> tmpSet1 = SetBlackListPart.getBlackListDirByPlatform(platform);
		if (tmpSet1 == null) {
			tmpSet1 = new HashSet<String>();
		}
		
		Set<String> tmpSet2 = SetBlackListPart.getBlackListCaseByPlatform(platform);
		if (tmpSet2 == null) {
			tmpSet2 = new HashSet<String>();
		}
		String filePath = file.getParent().substring(ServerMain.getBasicPathLength() + 1);
		System.out.println("file.getParent() -> " + file.getParent());
		System.out.println("file.getParent() -> " + filePath);
		String[] fileNames = file.getName().split("\\.");
		if (!(tmpSet1.contains(filePath) || tmpSet2.contains(fileNames[0]) || TextUtils.isEmpty(fileNames[0]))) {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String fileContent = new String();		//局部变量，执行完函数内容被清理
			String tmpStr;
			while((tmpStr = br.readLine()) != null) {
				fileContent = fileContent + tmpStr;
			}
			ja.add(fileContent);
			br.close();
		}
	}
	
	private static void readFolder(File file, JSONArray ja, Platform platform) throws IOException {		//读取文件夹的数据内容
		System.out.println("readFolder path -> " + file.getAbsolutePath());
		Set<String> tmpSet = SetBlackListPart.getBlackListDirByPlatform(platform);
		if (tmpSet == null) {
			tmpSet = new HashSet<String>();
		}
		
		boolean readFlag = false;
		if (file.getPath().length() <= ServerMain.getBasicPathLength()) {
			readFlag = true;
		} else {
			String filePath = file.getPath().substring(ServerMain.getBasicPathLength() + 1);
			if (!tmpSet.contains(filePath)) {
				readFlag = true;
			}
		}
		
		if (readFlag) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					readFolder(files[i], ja, platform);		//递归读取
				} else {
					readFile(files[i], ja, platform);
				}
			}
		}
	}
}
